from appointment_app import models
from django.db.models import Q

# 按照条件查询检测点
def getCheckPlaceList(queryForm):
    res = (
        models.check_place.objects.filter(
            Q(province__startswith=queryForm["province"])
            & Q(city__startswith=queryForm["city"])
            & Q(region__startswith=queryForm["region"])
            & Q(street__startswith=queryForm["street"])
            & Q(place_name__startswith=queryForm["place_name"])
            & Q(longitude__startswith=queryForm["longitude"])
            & Q(latitude__startswith=queryForm["latitude"])
        )
        .all()
        .order_by("-id")
    )  # 从大到小逆序排序
    return res


# 根据uuid查询监测点信息
def getCheckPlaceByUUid(uuid):
    res = models.check_place.objects.filter(uuid=uuid).first()
    return res


# 根据 provinc，city，region，street,place_name模糊查询查询检点的uuid集合
def getCheckPlaceByUuid(queryForm):
    res = (
        models.check_place.objects.filter(
            Q(province__startswith=queryForm["province"])
            & Q(city__startswith=queryForm["city"])
            & Q(region__startswith=queryForm["region"])
            & Q(street__startswith=queryForm["street"])
            & Q(place_name__startswith=queryForm["place_name"])
        )
        .all()
        .order_by("-id")
    )
    return res


# 根据uuid 查询省、市、区、街道、具体位置信息
def getCheckPlaceAllName(uuid):
    pccs = []
    place_name = ""
    res = models.check_place.objects.filter(uuid=uuid).first()
    if res:
        province = res.province
        city = res.city
        region = res.region
        street = res.street
        place_name = res.place_name
        pccs = [province, city, region, street]

    return pccs, place_name


# 根据pccs，省 市 区 街道查询检测点信息
def getcheckPlaceByPcrs(province, city, region, street):
    res = models.check_place.objects.filter(
        province=province, city=city, region=region, street=street
    )
    return res


# 增加检测点
def addCheckPlace(formData):
    res = models.check_place.objects.create(**formData)
    return res


# 更新监测点
def updateCheckPlace(formData):
    res = models.check_place.objects.filter(uuid=formData["uuid"]).update(**formData)
    return res


# 删除预约点
def delCheckPlace(uuid):
    res = models.check_place.objects.filter(uuid=uuid).delete()
    return res
